<?php

namespace App\Models\Applet\User;

use Illuminate\Support\Facades\DB;

class UserModel extends BaseUserModel
{
    protected $table = 'users';

    /**
     * 根据手机号获取用户信息
     * @param string $phone
     * @param array $fields
     * @return array
     */
    public function getItemByPhone(string $phone, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $result = $this->select($fields)
                       ->where('phone', $phone)
                       ->where('deleted', self::DELETED_NO)
                       ->first();
        return $result ? $result->toArray() : [];
    }

    /**
     * 根据邀请码查询用户
     * @param string $inviteCode
     * @param array $fields
     * @return array
     */
    public function getItemByInviteCode(string $inviteCode, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $result = $this->select($fields)
                       ->where('invite_code', $inviteCode)
                       ->where('deleted', self::DELETED_NO)
                       ->first();
        return $result ? $result->toArray() : [];
    }

    /**
     * 根据邀请人id查询总数
     * @param int $userId
     * @return mixed
     */
    public function getTotalByInviteUserId(int $userId)
    {
        return $this->where('invite_user_id', $userId)
                    ->where('deleted', self::DELETED_NO)
                    ->count();
    }


    /**
     * 根据邀请人id查询用户
     * @param int $userId
     * @param array $fields
     * @return array
     */
    public function getItemByInviteUserId(int $userId, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        return $this->select($fields)
                    ->where('invite_user_id', $userId)
                    ->where('deleted', self::DELETED_NO)
                    ->get()
                    ->toArray();
    }

    /**
     * 根据邀请人id查询总数
     * @param array $userIds
     * @return array
     */
    public function getGroupTotalByInviteUserId(array $userIds): array
    {
        return $this->select(['invite_user_id', DB::raw('count(invite_user_id) as count')])
                    ->whereIn('invite_user_id', $userIds)
                    ->where('deleted', self::DELETED_NO)
                    ->groupBy('invite_user_id')
                    ->get()
                    ->toArray();
    }

    /**
     * 用户列表总条数
     * @return void
     */
    public function total(array $params)
    {
        $query = $this;
        if (!empty($params['keyword'])) {
            $query = $query->whereRaw('phone like "%' . $params['keyword'] . '%" or nickname like "%' . $params['keyword'] . '%"');
        }

        return $query->where('deleted', self::DELETED_NO)
                     ->count();
    }

    /**
     * 用户列表
     * @param array $params
     * @param array $fields
     * @return mixed
     */
    public function list(array $params, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $query = $this;
        if (!empty($params['keyword'])) {
            $query = $query->whereRaw('phone like "%' . $params['keyword'] . '%" or nickname like "%' . $params['keyword'] . '%"');
        }

        return $query->select($fields)
                     ->where('deleted', self::DELETED_NO)
                     ->orderBy('id', 'desc')
                     ->offset((CURRENT_PAGE - 1) * PAGE_SIZE)
                     ->limit(PAGE_SIZE)
                     ->get()
                     ->toArray();
    }

    /**
     * 发放积分
     * @return void
     */
    public function incrementIntegral(int $userId, int $integral)
    {
        $this->where('id', $userId)
             ->increment('integral', $integral);
    }
}
